#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define TEXTLEN 512     /* length of symbols in input */
#define NSYMBOLS 1024   /* number of symbol table entries */
#define NOREG -1        /* AST generate functions have no register to return */
#define NOLABEL -1      /* AST generate functions have no label to jump */

/*
 * Tokens
 */
enum {
    T_EOF, 
    T_PLUS, T_MINUS, T_STAR, T_SLASH, 
    T_EQ, T_NE, T_LT, T_GT, T_LE, T_GE,
    T_INTLIT, T_SEMI, T_ASSIGN, T_IDENT,
    T_LBRACE, T_RBRACE, T_LPAREN, T_RPAREN,

    /* keywords */
    T_PRINT, T_INT, T_IF, T_ELSE, T_WHILE,
    T_FOR, T_VOID, T_CHAR
};

/*
 * Token structure
 */
struct token {
    int token;
    int intvalue;
};

/*
 * AST node types
 * The first few line up with related tokens.
 */
enum {
    A_ADD = 1, A_SUBSTRACT, A_MULTIPLY, A_DIVIDE,
    A_EQ, A_NE, A_LT, A_GT, A_LE, A_GE,
    A_INTLIT, A_IDENT, A_LVIDENT, A_ASSIGN,
    A_GLUE, A_IF, A_PRINT, A_WHILE, A_FUNCTION,
    A_WIDEN
};

/*
 * Primitive types.
 */
enum {
    P_NONE, P_VOID, P_CHAR, P_INT
};

/*
 * Structural types.
 */
enum {
    S_VARIABLE, S_FUNCTION
};

/*
 * Abstract Syntax Tree structure
 */
struct ASTnode {
    int op;                 /* "operation" to be performed on this tree */
    int type;               /* type of any expressions this tree generates */
    struct ASTnode *left;   /* left child tree */
    struct ASTnode *mid;    /* middle child tree */
    struct ASTnode *right;  /* right child tree */
    union {
        int intvalue;       /* for A_INTLIT, the integer value */
        int id;             /* for A_IDENT, the symbol slot number */
    } v;
};

/*
 * Symbol table
 */
struct symtable {
    char *name;     /* name of a symbol */
    int type;       /* primitive type */
    int stype;      /* structural type */
};
